/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.stuwork.rest;

import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.stuwork.domain.Stuwork;
import me.zhengjie.modules.stuwork.repository.StuworkRepository;
import me.zhengjie.modules.stuwork.service.StuworkService;
import me.zhengjie.modules.stuwork.service.dto.StuworkQueryCriteria;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import me.zhengjie.utils.PageResult;
import me.zhengjie.modules.stuwork.service.dto.StuworkDto;
import org.springframework.web.multipart.MultipartFile;

/**
* @website https://eladmin.vip
* @author wzy
* @date 2024-12-12
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "stu管理")
@RequestMapping("/api/stuwork")
public class StuworkController {
    private int count;
    private final StuworkService stuworkService;


    private final StuworkRepository stuworkRepository;
    public void importData1(MultipartFile file) throws IOException {
        List<Stuwork> stuworkList = new ArrayList<>();
        try (Workbook workbook = new XSSFWorkbook(file.getInputStream())) {
            Sheet sheet = workbook.getSheetAt(0); // 假设第一个 Sheet 存储数据
            System.out.println(sheet.getLastRowNum());
            for (int i = 1; i <= sheet.getLastRowNum(); i++) { // 跳过表头
                Row row = sheet.getRow(i);
                Stuwork stuwork = new Stuwork();
                System.out.println( row.getCell(0));
                System.out.println(row.getCell(1).getStringCellValue());
                System.out.println(row.getCell(2).getStringCellValue());
                System.out.println(row.getCell(3).getNumericCellValue());
                System.out.println(row.getCell(4).getStringCellValue());
                System.out.println(row.getCell(5).getStringCellValue());
                System.out.println(row.getCell(6).getStringCellValue());
                System.out.println(row.getCell(7).getStringCellValue());
                System.out.println(row.getCell(8).getStringCellValue());
                System.out.println(row.getCell(9).getStringCellValue());
                System.out.println(row.getCell(10).getStringCellValue());
                stuwork.setClassid(Integer.valueOf((int) row.getCell(0).getNumericCellValue()));
                stuwork.setStuid(Integer.valueOf((int) row.getCell(0).getNumericCellValue()));
                stuwork.setRoomid((int) row.getCell(0).getNumericCellValue());
                System.out.println(1);
                stuwork.setName(row.getCell(1).getStringCellValue());

                stuwork.setWeek(row.getCell(2).getStringCellValue());

                stuwork.setData(String.valueOf(row.getCell(3).getNumericCellValue()));

                stuwork.setSubmit(row.getCell(4).getStringCellValue());

                stuwork.setIsall(row.getCell(5).getStringCellValue());
                stuwork.setIslate(row.getCell(9).getStringCellValue());
                stuwork.setIsname(row.getCell(6).getStringCellValue());
                stuwork.setIsmatch(row.getCell(8).getStringCellValue());
                stuwork.setImaformation(row.getCell(10).getStringCellValue());
                stuwork.setIsright(row.getCell(7).getStringCellValue());
                stuwork.setScore(0);

                stuworkService.create(stuwork);


                System.out.println(1);
            }
        }
        // 保存解析的数据

    }
    @PostMapping("/import")
    @AnonymousAccess
    public ResponseEntity<Object> importData(@RequestParam("file") MultipartFile file
    ) {

        try {
            importData1(file);
            return new ResponseEntity<>(HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
        }
    }
    @PostMapping("/upsocre/{minAnswerCount}")
    @AnonymousAccess
    public ResponseEntity<Object> upData(@PathVariable("minAnswerCount") int minAnswerCount) {
        System.out.println(minAnswerCount);
        List<Stuwork> a =stuworkRepository.findBySubmit("是");
        for (Stuwork data : a) {

            System.out.println(a);
            data.setScore(minAnswerCount);
            stuworkRepository.save(data);
        }
        List<Stuwork> a1 =stuworkRepository.findBySubmit("否");
        for (Stuwork data : a1) {
            data.setScore(0);
            stuworkRepository.save(data);
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }
    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('stuwork:list')")
    public void exportStuwork(HttpServletResponse response, StuworkQueryCriteria criteria) throws IOException {
        stuworkService.download(stuworkService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询stu")
    @ApiOperation("查询stu")
    @PreAuthorize("@el.check('stuwork:list')")
    public ResponseEntity<PageResult<StuworkDto>> queryStuwork(StuworkQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(stuworkService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增stu")
    @ApiOperation("新增stu")
    @PreAuthorize("@el.check('stuwork:add')")
    public ResponseEntity<Object> createStuwork(@Validated @RequestBody Stuwork resources){
        stuworkService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改stu")
    @ApiOperation("修改stu")
    @PreAuthorize("@el.check('stuwork:edit')")
    public ResponseEntity<Object> updateStuwork(@Validated @RequestBody Stuwork resources){
        stuworkService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除stu")
    @ApiOperation("删除stu")
    @PreAuthorize("@el.check('stuwork:del')")
    public ResponseEntity<Object> deleteStuwork(@RequestBody Integer[] ids) {
        stuworkService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
